package niord.apps.adapters;

import niord.apps.helpers.Fonts;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.FilterQueryProvider;
import android.widget.Filterable;
import android.widget.TextView;

import com.niord.R;

/**
 * ContactListAdapter.
 */
public class ContactListAdapter extends CursorAdapter implements Filterable {
	/**
	 * Variables
	 */
	private ContentResolver mContent;

	public static final String[] CONTACT_PROJECTION = new String[] {
			Contacts._ID, Contacts.DISPLAY_NAME };

	private static final int COLUMN_DISPLAY_NAME = 1;

	public ContactListAdapter(Context context, Cursor c) {
		super(context, c);
		mContent = context.getContentResolver();
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final LayoutInflater inflater = LayoutInflater.from(context);
		final View view = (View) inflater.inflate(
				R.layout.dropdown_contact_list, parent, false);

		TextView textview = (TextView) view.findViewById(R.id.textView);

		// set cool font
		textview.setTypeface(Fonts.hattori);

		return view;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {

		((TextView) view).setText(cursor.getString(COLUMN_DISPLAY_NAME));
	}

	@Override
	public String convertToString(Cursor cursor) {
		return cursor.getString(COLUMN_DISPLAY_NAME);
	}

	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		FilterQueryProvider filter = getFilterQueryProvider();
		if (filter != null) {
			return filter.runQuery(constraint);
		}

		Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
				Uri.encode(constraint.toString()));
		return mContent.query(uri, CONTACT_PROJECTION, null, null, null);
	}
}
